selected = null;
t = null;
function hideNum()
{
	$("#num").fadeOut();
	selected.text("");
	if (selected != null)
	{
		selected.attr("class", "sudoku-td");
	}
}

$(".sudoku-td").click(function ()
{
	if (selected != null)
	{
		selected.attr("class", "sudoku-td");
	}
	selected = $(this);
	selected.attr("class", "sudoku-td-selected");
	$("#num").fadeIn();
	$("#num").offset(function ()
	{
		pos = new Object();
		pos.left = selected.offset().left + 41;
		pos.top = selected.offset().top - 70;
		return pos;
	}
	);
}
);

$(".input-td").click(function ()
{
	var flag=true;
	if (selected != null)
	{
		selected.text($(this).text());
		selected.attr("class", "sudoku-td");
		for (i=0;i<81;i++)
		{
			if ($("#T"+i+"T").text()==selected.text())
			{
				$("#T"+i+"T").fadeOut();
				$("#T"+i+"T").fadeIn();
				if ($("#T"+i+"T").text()=="")
					flag=false;
			}
		}
		if (flag==true)
		{
			//finish
			sudokuSolveSubmit();
		}
	}
	$("#num").fadeOut();
}
);

function countTime()
{
	var time = parseInt($("#time").val());
	if (!$("#detailModal").is(":visible"))
	{
		time++;
	}
	var h = parseInt(time / 3600);
	var m = parseInt(((time - h * 3600) - time % 60) / 60);
	var s = time - h * 3600 - m * 60;

	$("#time").val(time);
	$("#showTime").text(h + ":" + m + ":" + s);
	t = setTimeout("countTime()", 1000);
}

function sudokuSubmit()
{
	var sudoku='';
	for (i=0;i<81;i++)
	{
		sudoku=sudoku+($("#T"+i+"T").text()==""?"0":$("#T"+i+"T").text());
	}
	$("#getsudoku").hide();
	$("#getsudoku").load("getnowsudokusolve?sudoku="+sudoku);
	$("#getsudoku").fadeIn(2000);
}

function sudokuReset()
{
	for (i=0;i<81;i++)
	{
		$("#T"+i+"T").attr("class", "sudoku-td");
		$("#T"+i+"T").text("");
	}
	$(".sudoku-td").click(function ()
	{
		if (selected != null)
		{
			selected.attr("class", "sudoku-td");
		}
		selected = $(this);
		selected.attr("class", "sudoku-td-selected");
		$("#num").fadeIn();
		$("#num").offset(function ()
		{
			pos = new Object();
			pos.left = selected.offset().left + 47;
			pos.top = selected.offset().top - 70;
			return pos;
		}
		);
	}
	);
}

function sudokuAddSubmit()
{
	var sudoku='';
	for (i=0;i<81;i++)
	{
		sudoku=sudoku+($("#T"+i+"T").text()==""?"0":$("#T"+i+"T").text());
	}
	$("#content").val(sudoku);
	$("#addform").submit();
}

function sudokuSolveSubmit()
{
	var sudoku='';
	for (i=0;i<81;i++)
	{
		sudoku=sudoku+($("#T"+i+"T").text()==""?"0":$("#T"+i+"T").text());
	}
	
	var url="getsudokucheck?sid="+$("#sudoku_id").val()+"&content="+sudoku;
	$.get(url,function(data,status){
		alert(data);
		if (data=="true")
		{
			alert("success!");
			
		}else{
			alert("try again!");
		}
	});
}
